package br.com.unip.persistencia;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.unip.modelo.Participa;

public class ParticipaDao extends Dao {
	public void gravar(Participa p) throws Exception {
		abrirBanco();
		String query = "INSERT INTO participa (cod_proj, cod_func, horas_trab) VALUES (?, ?, ?)";
		pstmt = con.prepareStatement(query);
		System.out.println("Comando SQL => " + pstmt);
		pstmt.setInt(1, p.getCod_proj());
		pstmt.setInt(2, p.getCod_func());
		pstmt.setFloat(3, p.getHoras_trab());
		pstmt.execute();
		fecharBanco();
	}
	
	public void deletar(int id1, int id2) throws Exception {
		abrirBanco();
		String query = "DELETE FROM participa WHERE participa.cod_proj = ? AND participa.cod_func = ?";
		pstmt = con.prepareStatement(query);
		System.out.println("Comando SQL => " + pstmt);
		pstmt.setInt(1, id1);
		pstmt.setInt(2, id2);
		pstmt.execute();
		fecharBanco();
	}
	
	public List<Participa> listar() throws Exception {
		abrirBanco();
		String query = "SELECT * FROM participa";
		pstmt = con.prepareStatement(query);
		System.out.println("Comando SQL => " + pstmt);
		ResultSet rs = pstmt.executeQuery();
		Participa p = null;
		List<Participa> listar = new ArrayList<Participa>();
		while (rs.next()) {
			p = new Participa();
			p.setCod_proj(rs.getInt("cod_proj"));
			p.setCod_func(rs.getInt("cod_func"));
			p.setHoras_trab(rs.getFloat("horas_trab"));
			listar.add(p);
		}
		fecharBanco();
		return listar;
	}
}
